package pbN;

import java.util.ArrayList;
import java.util.List;

public class NumereMagice {

	/**
	 * @param args
	 */
	public static void iterativ(int n){
		int m = 0;
		
	      m=n*n;
		int a[] = new int[10];
		int j=0;
		for (int i = 0; i <= 9; i++) {
			
			int aux = m;
			while (aux > 0) {
				int cif = aux % 10;
			
				if (cif == i) {
					
					aux = 0;
					a[j] = i;
					j++;
				} else {
					aux = aux / 10;
				}

			}
		}
			if(j==10){
			System.out.println("numar magic");
		System.out.println(n+" "+m);
		
		}


		
	}
	public static void recursiv(int n,int a[]){
		

		int aux=n;
	     int m=aux%10;
	     aux=aux/10;
	     
	      for(int i=0;i<10;i++){
	    	  if(m==i){
	    		  a[m]=m;//il p 5 p poz lu 5
	    	  }
	    	 
	      }
	      if(aux>0){
	    	  recursiv(aux, a);//am taiat o cifra din numasr..si fac iar operatiunea
	      }
	    	      
	      
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//int n = 0;
		int a[] = new int[10];
		for (int i=10000;i<100000;i++){
			iterativ(i);
		}
		for (int i=10000;i<100000;i++){
		//iterativ(i);
			for(int j=0;j<10;j++){
				a[j]=-1;//golesc vectoru
			}
			int k=i*i;
			recursiv(k, a);
			
	        int corect=0;
			for(int j=0;j<10;j++){
	    		  if(a[j]==j){
	    			 corect++; //verific dak in a snt toate cifrele
	    		  }
	    		
	    		 
		    			
		    	  }
			 if(corect==10){
		
	    			System.out.println(i+" "+i*i); 
	    			
	    			
			}
		}

	}
		
}
